/// @tags: ICPC Shenyang
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

typedef long long ll;

ll solve(ll n, ll m, ll k) {
  ll ans = (k - 1) % (n - m + 1);
  if (k == 1) return m - 1;
  if (m <= 1e4) {
    for (ll i = 1, j = n - m + 2; i < m; ++i, ++j) {
      ans = (ans + k) % j;
    }
  } else {
    ll j = n - m + 2, i = 1;
    while (i < m) {
      if (ans + k >= j) {
        ans = (ans + k) % j;
        ++j;
        ++i;
        continue;
      }
      ll cnt = std::min(m - i, (j - 1 - ans) / k);
      i += cnt;
      j += cnt;
      ans += cnt * k;
    }
  }
  return ans;
}

int main() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; ++i) {
    ll n, m, k;
    cin >> n >> m >> k;
    cout << "Case #" << i << ": " << solve(n, m, k) + 1 << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("GYM101955K Let the Flames Begin.in", "r", stdin);
  freopen("GYM101955K Let the Flames Begin.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
